package com.demo.FileOutputStream_;

import org.apache.log4j.Logger;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author gy
 * @date 2022/4/4
 */
public class FileCopy {
    private static final Logger logger = Logger.getLogger(FileCopy.class);

    @Test
    public void m1() {

        String srcFilePath = "f:\\20220403\\壁纸.png";
        // 目标路径
        String targetFilePath = "d:\\20220403";

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        byte[] aByte = new byte[1024];
        int readLength = 0;
        try {
            fileInputStream = new FileInputStream(srcFilePath);
            fileOutputStream = new FileOutputStream(targetFilePath);

            while ((readLength = fileInputStream.read(aByte)) != -1){
                // 边读边写
                fileOutputStream.write(aByte,0,readLength);
            }
            logger.info("文件"+srcFilePath+"拷贝成功");

        } catch (IOException e) {
            logger.error(e.getMessage());
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
